;
var PLATFORM = PLATFORM || {};

// popup显示
PLATFORM.popupShow = function(dom) {
    $(dom).show();
};
// popup隐藏
PLATFORM.popupClose = function() {
    $(".u-popup .btn-close").click(function() {
        $(this).parents(".u-popup").hide()
    });
};

PLATFORM.pageTimeout = function() {
    $(document).ajaxSuccess(function(e, xhr, settings, data) {
        if(data != null && data.idsIntercepted == true) {
            PLATFORM.headTip("页面登录超时，请重新登录");
        }
    });
};
// 日期选择 年-月-日 时:分:秒
PLATFORM.timepickerRange = function(from, to) {
    $(from).datetimepicker({
        showSecond: true,
        timeFormat: 'hh:mm:ss',
        onClose: function(selectedDate) {
            $(to).datepicker("option", "minDate", selectedDate);
        }
    });
    $(to).datetimepicker({
        showSecond: true,
        timeFormat: 'hh:mm:ss',
        onClose: function(selectedDate) {
            $(from).datepicker("option", "maxDate", selectedDate);
        }
    });
};
// 日期选择 年-月-日
PLATFORM.datepickerRange = function(from, to) {
    $(from).datepicker({
        dateFormat: "yy-mm-dd",
        onClose: function(selectedDate) {
            $(to).datepicker("option", "minDate", selectedDate);
        }
    });
    $(to).datepicker({
        dateFormat: "yy-mm-dd",
        onClose: function(selectedDate) {
            $(from).datepicker("option", "maxDate", selectedDate);
        }
    });
};
/*
 * 对最小时间/最大时间做限制
 * count: 1最小时间明天，-1昨天，0今天
 */
PLATFORM.datepickerRangelimit = function(from, mincount, to) {
    $(from).datepicker({
        dateFormat: "yy-mm-dd",
        onClose: function(selectedDate) {
            if(getMinDate(0) > selectedDate) {
                $(to).datepicker("option", "minDate", getMinDate(0));
            } else {
                $(to).datepicker("option", "minDate", selectedDate);
            }
            var mm = new Date(selectedDate).getMonth();
            $(to).datepicker("option", "maxDate", getLastDay(mm));
        },
        minDate: getFirstDay()
    });
    $(to).datepicker({
        dateFormat: "yy-mm-dd",
        //      onClose: function(selectedDate) {
        //          $(from).datepicker("option", "maxDate", selectedDate);
        //      },
        maxDate: getLastDay(),
        minDate: getMinDate(0)
    });
};

PLATFORM.showTips = function(type, msg) {
    $("#popup_tips").show().find(".container p").text(msg);
    var tipicon = $("#popup_tips").find(".container div");
    if(type == "success") {
        tipicon.removeClass("error").removeClass("warning").addClass("success");
    } else if(type == "error") {
        tipicon.removeClass("success").removeClass("warning").addClass("error");
    } else if(type == "warning") {
        tipicon.removeClass("error").removeClass("success").addClass("warning");
    }
};
//获取URL传过来的参数
PLATFORM.getUrlArgs = function() {
    var args = {},
        query = location.search.substring(1),
        pairs = query.split("&"),
        pos = idx = argname = val = null;

    for(var i = 0; i < pairs.length; i++) {
        pos = pairs[i];
        idx = pos.indexOf("=");
        if(idx == -1) continue;
        argname = pos.substring(0, idx);
        val = decodeURIComponent(pos.substring(idx + 1));
        args[argname] = val;
    }

    return args;
};
/*
 * 数据加载慢时需要显示loading效果
 */
PLATFORM.setLoadingDom = function($Dom) {
        var loadingDom = '<div class="la-ball-spin-clockwise-fade">' +
            '<div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div>' +
            '</div>';
       $(".loadData").html(loadingDom)
        
};
PLATFORM.imgprefix = function (val) {
    var res = 0,
    len = val.length;
    if (val.indexOf("/uimg/") >= 0) {
        for (var i = 0; i < len; i++) {
            res = res * 31 + val.charCodeAt(i);
        }
        val = val.replace("?", res % 5 == 0 ? "" : res % 5);
        return val;
    } else {
        return val;
    }
};

    //1明天，-1昨天，0今天
function getMinDate(count) {
    var dd = new Date();
    dd.setDate(dd.getDate() + count);
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1; //获取当前月份的日期 
    var d = dd.getDate();
    return y + "-" + (m > 10 ? m : '0' + m) + "-" + (d > 10 ? d : '0' + d);
}

function getFirstDay() {
    var dd = new Date();
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1; //获取当前月份
    var d = "1";
    return y + "-" + (m > 10 ? m : '0' + m) + "-" + (d > 10 ? d : '0' + d);
}

function getLastDay(mm) {
    var dd = new Date();
    var y = dd.getFullYear();
    var m = parseInt(mm == undefined ? dd.getMonth() : mm, 10) + 1; //获取当前月份
    var day = new Date(y, m, 0);
    var d = day.getDate();
    return y + "-" + (m > 10 ? m : '0' + m) + "-" + (d > 10 ? d : '0' + d);
}
$(function() {
    PLATFORM.popupClose();
    //PLATFORM.pageTimeout();
    PLATFORM.setLoadingDom($(".loadData"));
});
